package com.mathlearning.auth.repository;

import com.mathlearning.auth.entity.LearningCourse;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public interface LearningCourseRepository extends JpaRepository<LearningCourse, Long> {
    Page<LearningCourse> findByCategoryIdAndDifficultyAndStatus(Long categoryId, String difficulty, String status, Pageable pageable);
    Page<LearningCourse> findByCategoryIdAndStatus(Long categoryId, String status, Pageable pageable);
    Page<LearningCourse> findByDifficultyAndStatus(String difficulty, String status, Pageable pageable);
    Page<LearningCourse> findByStatus(String status, Pageable pageable);

    @Modifying
    @Transactional
    @Query("update LearningCourse c set c.studentCount = c.studentCount + 1 where c.id = ?1")
    void incrementStudentCount(Long courseId);

    @Modifying
    @Transactional
    @Query("update LearningCourse c set c.videoCount = ?2 where c.id = ?1")
    void updateVideoCount(Long courseId, Integer videoCount);
}


